﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace DSServer
{
    public partial class Program
    {
        public static List<CClient> clients;
        public static TcpListener listener;
        public static int port = 17112;
        public static bool online = true;
        public static bool quit = false;

        static void Main(string[] args)
        {
            Program prog = new Program();
        }

        public Program()
        {
            clients = new List<CClient>();

            Thread listenThread = new Thread(new ThreadStart(ListenThread));
            listenThread.Start();

            while (!quit)
            {
                DoCommand(Console.ReadLine());
            }

        }

        public static void ListenThread()
        {
            listener = new TcpListener(IPAddress.Any, port);
            listener.Start();

            Console.WriteLine("Server started.");

            while (online)
            {
                Thread.Sleep(100);          // Loop sucks up all CPU cycles without this sleep
                if (listener.Pending())
                {
                    clients.Add(new CClient(listener.AcceptTcpClient()));
                    CClient cli = clients[clients.Count - 1];
                    CClientHandler handler = new CClientHandler(cli);     // Spawns new client handling thread
                }
            }

            listener.Stop();
        }

        public static string GetClientList()
        {
            string clientList = "";
            foreach (CClient cl in Program.clients)
            {
                clientList += cl.username + ", ";
            }
            clientList = clientList.Remove(clientList.Length - 2);          // Remove the last comma and space off the string
            
            return clientList;
        }

        public static void SendToAll(string msg)
        {
            foreach (CClient client in clients)
            {
                SendToUser(client, msg);
            }
        }
        public static void SendToUser(CClient client, string msg)
        {
            StreamWriter sw = new StreamWriter(client.socket.GetStream());

            sw.WriteLine(msg);
            sw.Flush();
        }
        public static void SendToUserByUsername(string username, string msg)
        {
            foreach (CClient cl in clients)
            {
                if (cl.username == username)
                {
                    SendToUser(cl, msg);
                    break;
                }
            }
        }

        public static void DropClient(string username)
        {
            // You can't remove the client from the collection inside the foreach loop
            // so find it in the loop and then remove it if one is found.
            CClient toRemove = null;
            foreach (CClient cl in clients)
            {
                if (cl.username == username)
                {
                    toRemove = cl;
                    break;
                }
            }
            
            if(toRemove != null)
                clients.Remove(toRemove);
        }
    }

    
}
